﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Day
{
    public class AccountDayTradeProduct
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public virtual TradeHistory TradeHistory { get; set; }
        /// <summary>
        /// 交易品种
        /// </summary>
        public int ProductId { get; set; }
        /// <summary>
        /// 交易品种
        /// </summary>
        public virtual Product Product { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public int Direction { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }



        ///// <summary>
        ///// 删除当日所有记录
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        //public void Delete(int tradeHistoryId, DB.DBManager db)
        //{
        //    var query = from t in db.AccountDayTradeProduct where t.TradeHistoryId == tradeHistoryId select t;
        //    foreach (Data.Day.AccountDayTradeProduct c in query)
        //    {
        //        db.AccountDayTradeProduct.Remove(c);
        //    }
        //    db.SaveChanges();
        //}


        ///// <summary>
        ///// 保存账号日变化情况
        ///// </summary>
        ///// <param name="tradeHistoryId">插入时间编号</param>
        ///// <param name="pdate">插入时间</param>
        //public void InitDayAccountProductChange(int tradeHistoryId, string pdate, DB.DBManager db)
        //{
        //    List<Data.ServerData.T_Account> serverAccounts = new Data.ServerData.T_Account().GetList();
        //    foreach (Data.ServerData.T_Account account in serverAccounts)
        //    {
        //        Data.ServerData.T_TradeDetail_History tth = new Data.ServerData.T_TradeDetail_History();
        //        List<string> products = tth.GetAccountDayProduct(pdate, account.Id);
        //        foreach (string product in products)
        //        {
        //            #region i为0是多，i为1是空
        //            for (int i = 0; i < 2; i++)
        //            {
        //                Data.Day.AccountDayTradeProduct change = new Data.Day.AccountDayTradeProduct();
        //                Sys.Account _account = new Sys.Account();
        //                change.AccountId = _account.GetAccountId(account.LoginAccount);
        //                change.TradeHistoryId = tradeHistoryId;
        //                change.Direction = i;
        //                change.BillCount = tth.GetDayProductBillCount(pdate, account.Id, product, change.Direction.ToString(), db);
        //                change.Free = tth.GetDayProductCommission(pdate, account.Id, product, change.Direction.ToString(), db);
        //                change.ProductId = new Sys.Product().GetProductId(product, db);
        //                change.LoseCount = tth.GetDayProductLoseCount(pdate, account.Id, product, change.Direction.ToString(), db);
        //                change.Profit = tth.GetDayProductCloseProfit(pdate, account.Id, product, change.Direction.ToString(), db);
        //                change.DayCount = change.Profit - change.Free;
        //                change.SumLost = tth.GetDayProductAllLose(pdate, account.Id, product, change.Direction.ToString(), db);
        //                change.SumWin = tth.GetDayProductAllWin(pdate, account.Id, product, change.Direction.ToString(), db);
        //                change.UsedMargin = new BenefitHelper.Data.ServerData.T_PositionDetail_History().GetAccountProduct(account.Id, new Sys.TradeHistory().GetPdateFromSettlementId(tradeHistoryId, db), product, db);
        //                change.WinCount = tth.GetDayProductWinCount(pdate, account.Id, product, change.Direction.ToString(), db);
        //                db.AccountDayTradeProduct.Add(change);
        //            }
        //            #endregion
        //        }
        //    }
        //    db.SaveChanges();
        //}



        //public List<AccountDayTradeProduct> GetAccountDayTradeProduct(int accountid, int tradeHistoryId, DB.DBManager db)
        //{
        //    List<AccountDayTradeProduct> instruments = new List<AccountDayTradeProduct>();
        //    var query = db.AccountDayTradeProduct.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
        //    if (query.Count() > 0)
        //    {
        //        instruments = query.ToList();
        //    }
        //    return instruments;
        //}

        //public List<AccountDayTradeProduct> GetAccountDayTradeProduct(int accountid, int tradeHistoryId, bool win, DB.DBManager db)
        //{
        //    List<AccountDayTradeProduct> instruments = new List<AccountDayTradeProduct>();
        //    List<AccountDayTradeProduct> query = null;
        //    if (win)
        //    {
        //        query = db.AccountDayTradeProduct.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.Profit > 0).ToList();

        //    }
        //    else
        //    {
        //        query = db.AccountDayTradeProduct.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.Profit <= 0).ToList();
        //    }
        //    if (query.Count() > 0)
        //    {
        //        instruments = query.ToList();
        //    }
        //    return instruments;
        //}
    }
}